﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using Models.Constant;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class SceneController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        // GET: Mager_Department
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " and s_c_guid='" + LoginUserInfo.userCompanyGuid + "' ";
            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and s_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));

            }
            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            if (!string.IsNullOrEmpty(httpRequest.status))
            {
                //设置状态
                sqlwhere = sqlwhere + " and s_status=@status";
                sugarParameters.Add(new SugarParameter("status", httpRequest.status));
            }


            string sql = "select  * from iot_product where 1= 1  " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "s_id desc", ref totalRecord);


            ViewData["DataList"] = dt;

            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码
            return View(httpRequest);
        }
        public IActionResult edit(HttpReqModel httpReqModel)
        {
            if (httpReqModel.order == "upd")
            {
                ViewBag.tp_guid = httpReqModel.guid.ToString();
            }
            else
            {
                ViewBag.tp_guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;
            return View();
        }

        public RespResultData saveAll(IFormCollection FC, HttpReqModel httpReqModel, iot_device_task_plain_item reqItem, iot_device_task_plain req)
        {
            req.tp_c_guid = LoginUserInfo.userCompanyGuid;
            reqItem.tpi_tp_guid = req.tp_guid;
            if (httpReqModel.order != "upd")
            {
                reqItem.tpi_guid = Guid.NewGuid().ToString();

            }
            if (reqItem.tpi_type == 0)
            {
                reqItem.tpi_taskobj_guid = FC["seldevice"];
                reqItem.tpi_key = new DBIotDeviceCommand().getCommand(FC["selcommand"].ToString()).dc_key;
            }
            else if (reqItem.tpi_type == 100)
            {
                reqItem.tpi_taskobj_guid = ConstantDevice.DELAY_DEVICE_GUID;// "368767c7-2502-4614-b337-ae33094459c4";
                reqItem.tpi_key = "delay";
                reqItem.tpi_value = FC["delaysecond"];
            }
            else if (reqItem.tpi_type == 2)
            {
                reqItem.tpi_taskobj_guid = FC["seldevice2"];
                reqItem.tpi_key = new DBIotDeviceVariable().getVariable(FC["selvar"].ToString()).v_key;
                reqItem.tpi_value = FC["conditionValue"].ToString();
                reqItem.tpi_value_rlue = new DBIotDeviceVariableValueRlue().getVariableValueRlue(FC["selvarvalue"].ToString()).vr_rule;
            }
            //else if (reqItem.tpi_type == 1)
            //{
            //    reqItem.tpi_taskobj_guid = FC["selLinkage"];
            //    reqItem.tpi_value = FC["delaysecond"];
            //}
            new DBIotDeviceTaskPlainItem().saveAll(reqItem);
            new DBIotDeviceTaskPlain().saveAll(req);

            return RespResult.Sucess();
        }
        public JsonResult saveplain(IFormCollection FC, HttpReqModel httpReqModel, iot_device_task_plain plain)
        {
            new DBIotDeviceTaskPlain().saveAll(plain);
            return ReturnJsonResult.Sucess();
        }
        public RespResultData getPlain(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDeviceTaskPlain().getPlain(httpRequest.guid));
        }


        public RespResultData delItem(IFormCollection FC, HttpReqModel httpReqModel, iot_device_task_plain_item reqItem, iot_device_task_plain req)
        {

            new DBIotDeviceTaskPlainItem().delItem(httpReqModel.guid);

            return RespResult.Sucess();
        }

        public RespResultData getDevice(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDevice().getDevice(httpRequest.guid));
        }


        public RespResultData getLinkageList(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDeviceLinkage().getLinkageList(LoginUserInfo.userCompanyGuid, 0));
        }
        public RespResultData getPlainItemList(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBIotDeviceTaskPlainItem().getPlainItemList(httpRequest.guid));
        }

        public JsonResult getCommandList(HttpReqModel httpReqModel)
        {
            iot_device device = new DBIotDevice().getDevice(httpReqModel.guid);

            return ReturnJsonResult.Sucess(new DBIotDeviceCommand().getCommandList(device.d_model_guid));
        }

        public JsonResult getHasVariableDevice(HttpReqModel httpReqModel)
        {
            return ReturnJsonResult.Sucess(new DBIotDeviceVariable().getHasVariableDevice(LoginUserInfo.userCompanyGuid));
        }
        public JsonResult getVariableByDevice(HttpReqModel httpReqModel)
        {
            return ReturnJsonResult.Sucess(new DBIotDeviceVariable().getVariableByDevice(LoginUserInfo.userCompanyGuid, httpReqModel.guid, httpReqModel.export));
        }
        public JsonResult getVariableValueRlue(HttpReqModel httpReqModel)
        {
            List<iot_device_model_variable> iot_Device_Variables = new DBIotDeviceVariable().getVariableByGuid(LoginUserInfo.userCompanyGuid, httpReqModel.guid, httpReqModel.export);
            if (iot_Device_Variables.Count > 0)
            {
                return ReturnJsonResult.Sucess(new DBIotDeviceVariableValueRlue().getVariableValueRlueList(iot_Device_Variables[0].v_value_type));
            }
            return ReturnJsonResult.Faild();
        }
    }
}